package com.love_weinuo.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.bean.LandNameBean;
import com.love_weinuo.bean.NearPindanCallBackBean;
import com.love_weinuo.databinding.ItemIndexBinding;
import com.love_weinuo.databinding.ItemIndexHeadBinding;
import com.love_weinuo.databinding.ItemIndexSecondBinding;
import com.love_weinuo.ui.view.CycleViewPager;
import com.love_weinuo.ui.view.Info;
import com.love_weinuo.ui.view.RxProgressBar;
import com.love_weinuo.util.glideutil.GlideUtil;
import com.love_weinuo.util.listener.OnSelectLandListener;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by SensYang on 2019/03/01 15:53
 */

public class IndexAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<NearPindanCallBackBean.ResultEntity.ResEntity> strings;
    List<Info.ResultEntity.ResEntity> imgs;
    private Context mcontext;
    LandNameBean bean;
    OnItemClickListener mOnItemClickLitener;
    int HEAD = -1;
    int Second = -2;
    OnSelectLandListener onSelectLandListener;

    public void setOnSelectLandListener(OnSelectLandListener listener) {
        this.onSelectLandListener = listener;
    }

    public IndexAdapter(Context context, List<NearPindanCallBackBean.ResultEntity.ResEntity> strings,
                        List<Info.ResultEntity.ResEntity> imgs, LandNameBean landNameBean) {
        this.mcontext = context;
        this.strings = strings;
        this.imgs = imgs;
        this.bean = landNameBean;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD) {
            ItemIndexHeadBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext), R.layout.item_index_head, parent, false);
            functionBinding.setAdapter(this);
            return new TitleViewHolder(functionBinding);
        } else if (viewType == Second) {
            ItemIndexSecondBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext), R.layout.item_index_second, parent, false);
            functionBinding.setAdapter(this);
            return new SecondViewHolder(functionBinding);
        } else {
            ItemIndexBinding functionBinding = DataBindingUtil.inflate(LayoutInflater.from(mcontext), R.layout.item_index, parent, false);
            functionBinding.setAdapter(this);
            return new TourViewHolder(functionBinding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        if (holder instanceof TitleViewHolder) {
            TitleViewHolder tvh = (TitleViewHolder) holder;
            tvh.cvpModule.setDelay(2000);
            tvh.cvpModule.setData(imgs, new CycleViewPager.ImageCycleViewListener() {
                @Override
                public void onImageClick(Info.ResultEntity.ResEntity info, int position, View imageView) {
                    //                    Intent intent = new Intent(mcontext, WebViewActivity.class);
                    //                    intent.putExtra("module_web_url", info.getUrl());
                    //                    mcontext.startActivity(intent);
                }
            });
        } else if (holder instanceof SecondViewHolder) {
            SecondViewHolder vh = (SecondViewHolder) holder;
            vh.tvModuleLandName.setText(bean.getLandName());
            vh.llModuleSelectLand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSelectLandListener.setOnSelectLandListener();
                }
            });
        } else if (holder instanceof TourViewHolder) {
            holder.bindData(strings.get(position - 2));
            final TourViewHolder tv = (TourViewHolder) holder;
            BigDecimal b = new BigDecimal((float) strings.get(position - 2).getHasland() / strings.get(position - 2).getSumarea());
            double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            tv.bar.setProgress((float) result * 100);
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos - 2);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(holder.itemView, pos - 2);
                        return false;
                    }
                });
            }
            tv.tvModuleStartTime.setText(strings.get(position - 2).getStarttime().split(" ")[0]);
            tv.tvModuleEndTime.setText(strings.get(position - 2).getEndtime().split(" ")[0]);
            tv.tvModuleResidue.setText((strings.get(position - 2).getSumarea() - strings.get(position - 2).getHasland()) + "");
            tv.tvModuleArea.setText(strings.get(position - 2).getHasland() + "");
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size() + 2;
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<NearPindanCallBackBean.ResultEntity.ResEntity> {
        ItemIndexBinding functionBinding;
        RxProgressBar bar;
        TextView tvModulePindan, tvModuleResidue, tvModuleArea, tvModuleEndTime, tvModuleStartTime, tvModuleStatue;

        public TourViewHolder(ItemIndexBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            bar = dataBinding.rpModuleProgressBar;
            tvModulePindan = dataBinding.tvModulePindan;
            tvModuleResidue = dataBinding.tvModuleResidue;
            tvModuleArea = dataBinding.tvModuleArea;
            tvModuleStartTime = dataBinding.tvModuleStartTime;
            tvModuleEndTime = dataBinding.tvModuleEndTime;
            tvModuleStatue = dataBinding.tvModuleStatue;
        }

        @Override
        public void bindData(NearPindanCallBackBean.ResultEntity.ResEntity data) {
            functionBinding.setBean(data);
        }
    }

    public class TitleViewHolder extends BaseRecyclerViewHolder<String> {
        ItemIndexHeadBinding binding;
        CycleViewPager cvpModule;


        public TitleViewHolder(ItemIndexHeadBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            cvpModule = binding.cvpModule;
        }

        @Override
        public void bindData(String data) {
            binding.setBean(data);
        }
    }

    public class SecondViewHolder extends BaseRecyclerViewHolder<LandNameBean> {
        ItemIndexSecondBinding binding;
        LinearLayout llModuleSelectLand;
        TextView tvModuleLandName;

        public SecondViewHolder(ItemIndexSecondBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            llModuleSelectLand = binding.llModuleSelectLand;
            tvModuleLandName = binding.landName;
        }

        @Override
        public void bindData(LandNameBean data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        } else if (position == 1) {
            return Second;
        } else {
            return strings.size();
        }
    }

    /**
     * 得到轮播图的View
     *
     * @param context
     * @param url
     * @return
     */
    public static View getImageView(Context context, String url) {
        RelativeLayout rl = new RelativeLayout(context);
        //添加一个ImageView，并加载图片
        ImageView imageView = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(layoutParams);
        //使用Picasso来加载图片
        GlideUtil.setCircleMethod(context, url, imageView);
        //在Imageview前添加一个半透明的黑色背景，防止文字和图片混在一起
        ImageView backGround = new ImageView(context);
        backGround.setLayoutParams(layoutParams);
        backGround.setBackgroundResource(R.color.cycle_image_bg);
        rl.addView(imageView);
        rl.addView(backGround);
        return rl;
    }

    public void toNext(View view, NearPindanCallBackBean.ResultEntity.ResEntity t) {

    }

    public void setLandname(String landname){
        bean.setLandName(landname);
        notifyItemChanged(1);
    }

}